package com.mjh.zhujianju.controller.agency;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.livew.framework.base.controller.BaseController;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedResult;
import com.livew.framework.pojo.AjaxResult;
import com.mjh.zhujianju.model.agency.DepartmentEO;
import com.mjh.zhujianju.service.agency.DepartmentService;
import com.mjh.zhujianju.vo.agency.DepartmentVO;

/***
 * 
 * @ClassName: DepartmentController
 * @Description 部门
 * @author wangdong
 * @date 2018年8月6日 下午11:20:57
 * @version V1.0
 */
@Controller
@RequestMapping("agency/dept")
public class DepartmentController extends BaseController<DepartmentEO> {

	@Resource
	private DepartmentService departmentService;

	@Override
	protected BaseService<DepartmentEO> getService() {

		return departmentService;
	}

	@RequestMapping(value = "create")
	public String create() {

		return "agency/dept/create";
	}

	@RequestMapping(value = "saveInfo", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult saveInfo(DepartmentVO vo) {
		if (vo == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		try {
			departmentService.saveInfo(vo);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(Boolean.TRUE, "保存成功");
	}

	@RequestMapping(value = "updateInfo", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult updateInfo(DepartmentVO vo) {
		if (vo == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		try {
			departmentService.updateInfo(vo);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(Boolean.TRUE, "保存成功");
	}

	/**
	 * 获取所有的组织机构
	 * 
	 * @Description:
	 * @return
	 * @author wt1293
	 * @date 2018年8月9日下午8:08:53
	 *
	 */
	@RequestMapping(value = "getPage", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult getPage(DepartmentEO eo) {
		PagedResult<DepartmentVO> list = departmentService.getVOList(eo);
		return AjaxResult.getResult(list);
	}

	/**
	 * 获取所有的组织机构（不分页）
	 *
	 * @Description:
	 * @return
	 * @author wt1293
	 * @date 2018年8月9日下午8:08:53
	 *
	 */
	@RequestMapping(value = "getDepartment", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResult getDepartment() {
		List<DepartmentVO> list = departmentService.queryDepartmentForList();
		return AjaxResult.getResult(list);
	}


	/**
	 * 重新获取详情
	 */
	public AjaxResult findById(@PathVariable("id") Long id) {
		if (id == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		DepartmentVO t = null;
		try {
			t = this.departmentService.getInfo(id);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(t);
	}

	/**
	 * 获取所有的组织机构
	 * 
	 * @Description:
	 * @return
	 * @author wt1293
	 * @date 2018年8月9日下午8:08:53
	 *
	 */
	@RequestMapping(value = "getAllList", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResult getAllList() {
		
		List<DepartmentEO> list = departmentService.getAll();
		return AjaxResult.getResult(list);
	}

}
